﻿using System;
using System.ComponentModel;
using System.Web.UI;
using System.Collections;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.Core;

namespace Yuanlin.Business
{
    public class AquacultureList : YuanlinUserControl
    {
        protected Repeater list;
        protected Pager pager;
        protected CurrentPage currentPage;
        private string queryText = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.QueryText);
        private string character = UrlManager.Instance.GetParameterValue<string>(YuanlinParameterName.Character);
        private int aquacultureKebieId = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.AquacultureKebieId);
        public int categoryType = UrlManager.Instance.GetParameterValue<int>(YuanlinParameterName.CategoryType);

        protected override void OnLoad(EventArgs e)
        {
            BindData();
        }

        public void BindData()
        {
            int aquacultureTotalCount = 0;
            EntityList dataSource = null;
            EntityList aquacultures = null;
            AquacultureListRequest request = new AquacultureListRequest();

            if (!string.IsNullOrEmpty(character))
            {
                EntityList currentPageAquacultures = new EntityList();
                EntityList resultAquacultures = new EntityList();

                request.CategoryType = categoryType;
                request.Title = queryText;
                aquacultures = BusinessManager.GetAquacultureList(RequestHelper.BuildGetAquacultureListRequest(request));
                foreach (Aquaculture aquaculture in aquacultures)
                {
                    string temp = ChineseConvertor.Convert(aquaculture.Title.Value);
                    if (!string.IsNullOrEmpty(temp) && temp[0].ToString().ToLower() == character.ToLower())
                    {
                        resultAquacultures.Add(aquaculture);
                    }
                }
                for (int i = pager.PageIndex * pager.PageSize; i < resultAquacultures.Count && i < (pager.PageIndex + 1) * pager.PageSize; i++)
                {
                    currentPageAquacultures.Add(resultAquacultures[i]);
                }
                dataSource = currentPageAquacultures;
                aquacultureTotalCount = resultAquacultures.Count;
            }
            else
            {
                request.CategoryType = categoryType;
                request.KebieId = aquacultureKebieId;
                request.Title = queryText;
                request.PageIndex = pager.PageIndex;
                request.PageSize = pager.PageSize;
                aquacultures = BusinessManager.GetAquacultureList(RequestHelper.BuildGetAquacultureListRequest(request));
                dataSource = aquacultures;
                aquacultureTotalCount = aquacultures.TotalCount;
            }

            list.DataSource = dataSource;
            list.DataBind();

            if (pager != null && currentPage != null)
            {
                pager.TotalPages = pager.CalculateTotalPages(aquacultureTotalCount);
                currentPage.TotalRecords = aquacultureTotalCount;
                currentPage.TotalPages = pager.TotalPages;
                currentPage.PageIndex = pager.PageIndex;
            }
        }

    }
}
